<?php

namespace App\Http\Controllers\admin;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\common\PublicController;
//模型
use App\Models\Building;
use App\Models\Room;
use App\Models\Buildingmember;

class BuildingController extends PublicController
{
    //建筑列表页面
    public function getIndex(Request $request)
    {
        /*搜索条件始*/
        //搜索条件
        $get['area'] = $request -> input('area','');
        if($get['area']){
            $this -> where .= ' -> where("area.name","like","%".$get["area"]."%")';
        }
        $get['building'] = $request -> input('building','');
        if($get['building']){
            $this -> where .= ' -> where("building.name","like","%".$get["building"]."%")';
        }
        /*搜索条件末*/
        /*代理商搜索始*/
        //获取自身及旗下所有代理商
        $agentInfo = $this -> agentInfo($request -> cookie('logon')['agent_id']);
        /*代理商搜索末*/
        //获取建筑信息
        $buildingInfo = $this -> rangeInfo('building',$get);
        $data = $this -> memberInRange('building',$buildingInfo -> all());
        $get['agent_id'] = $this -> agent_id;
        return view('admin.building.index',['data'=>$data,'buildingInfo'=>$buildingInfo,'agentInfo'=>$agentInfo,'request'=>$request->all(),'get'=>$get,'areaids' => $this->area_ids]);
    }

    //添加建筑页面
    public function getCreate()
    {
        //获取代理商下的所有区域
        if($this -> area_ids === '0'){
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> get();
        }else{
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> whereIn("area.id",explode(",",$this -> area_ids)) -> get();
        }
        return view('admin/building/create',['areaInfo' => $areaInfo]);
    }

    //添加处理页面
    public function postStore(Request $request)
    {
        $info = $request -> except('_token');
        $info['agent_id'] = $this -> agent_id;

        $infoBuil=[
            'name'=>$info['name'],
            'latitude'=>$info['latitude'],
            'longitude'=>$info['longitude'],
            'area_id'=>$info['area_id'],
            'agent_id'=>$info['agent_id']
        ];
        //添加建筑表
//        $res = Building::create($info);
        $buildId = DB::table('building')->insertGetId($infoBuil);
        //批量添加房间表
        if($info['numfloor']>0 && $info['numroom']> 0){
            for($i=1;$i<=$info['numfloor'];$i++){
                for($j=1;$j<=$info['numroom'];$j++){
                    //处理房间名字
                    if($j<10){
                        $roomName = $i.'0'.$j;
                    }else{
                        $roomName = $i.$j;
                    }
                    //房间存储信息整合
                    $infoRoom = [
                        'name'=>$roomName,
                        'agent_id'=>$info['agent_id'],
                        'building_id'=>$buildId,
                    ];
                    $rec = Db::table('room')->insert($infoRoom);
                }
            }
        }


        if($buildId){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    //编辑建筑页面
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        $data = Building::find($id) -> toarray();
        //获取代理商下的所有区域
        if($this -> area_ids === '0'){
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> get();
        }else{
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> whereIn("area.id",explode(",",$this -> area_ids)) -> get();
        }
        return view('admin.building.edit',['data'=>$data,'areaInfo'=>$areaInfo]);
    }

    //编辑处理页面
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        $info = $request -> except('id','_token');
        $res = Building::find($id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    //删除处理页面
    public function postDestroy(Request $request)
    {
        $id = $request -> input('id');
        //查询建筑下是否有房间 ,如果有不能删
        $count = Room::where('building_id',$id) -> count();
        if($count == 0){
            //查询区域和客户的关系表并删除
            Buildingmember::where(['building_id'=>$id]) ->delete();
            $res = Building::destroy($id);
            if($res){
                $data['status'] = 1;
            }else{
                $data['status'] = -1;
            }
        }else{
            $data['status'] = -2;
        }
        echo json_encode($data);
    }

    //追加负责人(客户)
    public function postAddmember(Request $request)
    {
        //获取post数据
        $building_id = $request -> input('id');
        $member = $request -> input('member');
        $info = $this -> addMember('building',$building_id,$member);
        echo json_encode($info);
    }

    //删除负责人(客户)
    public function postDelmember(Request $request)
    {
        //获取post数据
        $building_id = $request -> input('building_id');
        $member_id = $request -> input('member_id');
        //查询区域和客户的关系表并删除
        $info = $this -> delMember('building',$building_id,$member_id);
        echo json_encode($info);
    }
}
